package com.alumni_circle.controller.home;

import cn.hutool.core.util.StrUtil;
import com.alumni_circle.constant.PathConstant;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.DownloadPicture;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.PictureBase64;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.home.SlideShow;
import com.alumni_circle.entity.home.VO.SlideShowInsertVO;
import com.alumni_circle.entity.home.VO.SlideShowSearchVO;
import com.alumni_circle.entity.home.VO.SlideShowUpdateVO;
import com.alumni_circle.service.home.ISlideShowService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.alumni_circle.core.utils.Upload.getUUID;

/**
 * @author 龙小虬
 * @since 2020-07-06 22:58
 */
@Api(tags = "轮播图管理")
@Controller
@RequestMapping("/slideShow")
public class SlideShowController extends BaseController<SlideShow> {

    @Resource
    private ISlideShowService slideShowService;

    @ResponseBody
    @RequestMapping(value = "/addSlideShow")
    @ApiOperation(value = "新增轮播图信息", notes = "增加轮播图信息")
    @SysLog(type="增",accessInfo="新增轮播图信息",url="/slideShow/addSlideShow")
    public String upload(@RequestParam("file") MultipartFile file){
        SlideShow slideShow = new SlideShow();
        //若文件选择为空,就上传失败
        if(file.isEmpty()){
            return "上传失败,请选择文件!";
        }
        //获取文件上传的文件名
        String fileName=file.getOriginalFilename();
        //后缀
        assert fileName != null;
        String suffix = fileName.substring(fileName.lastIndexOf("."));

        fileName = fileName.substring(0,fileName.lastIndexOf("."));
        //指定到上传的文件路径
        String filePath = PathConstant.SERVER.getPath();
        if(!new File(filePath+fileName+suffix).exists()){
            File dir = new File(filePath);
            //若路径不存在,则创建一个这样的文件夹
            if (!dir.exists()) {
                dir.mkdir();
            }
            try {
                //创建了一个File对象，名字是file1 ，路径是filePath，名字是fileName。
                File file1 = new File(filePath, fileName+suffix);
                //然后就可以调用这个对象的相关方法完成文件创建，删除，读取，写入等操作
                // 将上传的文件写入创建好的文件中
                file.transferTo(file1);
//                System.out.println(fileName+"{-temporary}"+suffix);
                slideShowService.save(slideShow);
                return (fileName+suffix);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            //寻找不存在的命名
            while (new File(filePath+fileName).exists()){
                fileName = getUUID();
            }
            File dir = new File(filePath);
            //若路径不存在,则创建一个这样的文件夹
            if (!dir.exists()) {
                dir.mkdir();
            }
            try {
                //创建了一个File对象，名字是file1 ，路径是filePath，名字是fileName。
                File file1 = new File(filePath, fileName+suffix);
                //然后就可以调用这个对象的相关方法完成文件创建，删除，读取，写入等操作
                // 将上传的文件写入创建好的文件中
                file.transferTo(file1);
                slideShow.setPicture(fileName+suffix);
                slideShowService.save(slideShow);
                return (fileName+suffix);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return "fail";
    }
    /**
     * 按照顺序将需要进行设置，以分号分隔老数据顺序和新数据顺序，逗号分隔图片的id
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateSlideShow", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新轮播图信息", notes = "更新轮播图信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新轮播图信息",url="/slideShow/updateSlideShow")
    public ResponseModel updateSlideShow(@ApiParam(required = true, name = "SlideShowUpdateVO")
                                        @RequestBody BaseId baseId) {
        slideShowService.resetSequence();
        //更新播放顺序
        String[] str = baseId.getId().split(",");
        for (int i = 1; i <= str.length; i++) {
            SlideShow slideShow = new SlideShow();
            slideShow.setId(str[i-1]);
            slideShow.setSequence(String.valueOf(i));
            slideShowService.updateById(slideShow);
        }
        return new ResponseModel<>();
    }

    @ResponseBody
    @RequestMapping(value = "/selectSlideShow", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询轮播图信息", notes = "查询轮播图信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询轮播图信息",url="/slideShow/selectSlideShow")
    public ResponseModel selectSlideShow(@ApiParam(required = true, name = "SlideShowInsertVO")
                                        @RequestBody BaseId baseId) {

        SlideShow slideShow = slideShowService.getById(baseId.getId());
        return new ResponseModel<>(ModelUtils.transform(slideShow, SlideShowInsertVO.class));
    }

    @ResponseBody
    @RequestMapping(value = "/deleteSlideShow", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除轮播图信息", notes = "删除轮播图信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="新增评论信息",url="/slideShow/addslideShow")
    public ResponseModel deleteSlideShow(@ApiParam(required = true, name = "SlideShowInsertVO")
                                        @RequestBody BaseId baseId) {

        slideShowService.removeById(baseId.getId());
        return new ResponseModel();
    }


    @ResponseBody
    @RequestMapping(value = "/getAllSlideShow", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询符合条件轮播图信息", notes = "查询符合条件轮播图信息", httpMethod = "GET", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询符合条件轮播图信息",url="/slideShow/getAllSlideShow")
    public ResponseListModel getAllSlideShow() {
        List<SlideShowSearchVO>list = ModelUtils.copyListProperties(slideShowService.list(),SlideShowSearchVO.class);
        for (SlideShowSearchVO slideShowSearchVO : list) {
            slideShowSearchVO.setSequence("0");
        }
        String strings = slideShowService.viewSlideShow().toString();
        return new ResponseListModel<>(list,strings.substring(1,strings.length()-1));
    }

}
